#ifndef _PACKET_H
#define _PACKET_H

#include <QByteArray>

class Packet {
  public:

    /* Decodes, extracts and removes a Packet from the QByteArray buffer.
       Returns null and does nothing if there isn't a full packet.        */
    static Packet *readPacket(QByteArray *buffer);

    //Encodes a fixed size header for the packet using getPacketId and getBodySize templates.
    QByteArray getHeader();

    //Encodes the information of the packet in a QByteArray of getBodySize() bytes.

    virtual QByteArray getBody() = 0;

  protected:
    //Returns Packet type identifier.
    virtual int getPacketId() = 0;

    //Returns size of the Package body in bytes.
    virtual int getBodySize() = 0;

    //Decodes info from the body and set's it in the packet.
    virtual void setFromBuffer(QByteArray *buffer) = 0;

  public:
    //Casts to Message Packet. Returns null if it not possible.
    bool isMessagePacket();

    //Constant identifier for MessagePackets.
    static const int MessagePacketID;

};
#endif
